<template>
  <div id="app" class="d-flex flex-column min-vh-100">
    <!-- 导航栏 -->
    <TheNavbar />
    
    <!-- 主内容区域 -->
    <main class="flex-grow-1">
      <router-view v-slot="{ Component, route }">
        <transition name="page" mode="out-in">
          <component :is="Component" :key="route.path" />
        </transition>
      </router-view>
    </main>
    
    <!-- 底部 -->
    <TheFooter />

    <!-- 加载指示器 -->
    <div v-if="isLoading" class="loading-overlay">
      <div class="loading-spinner">
        <div class="spinner-border text-primary" role="status">
          <span class="visually-hidden">Loading...</span>
        </div>
        <p class="mt-3">加载中...</p>
      </div>
    </div>

    <!-- 回到顶部按钮 -->
    <div 
      v-show="showBackToTop" 
      class="back-to-top-btn"
      @click="scrollToTop"
      title="返回顶部"
    >
      <i class="bi bi-arrow-up"></i>
    </div>
  </div>
</template>

<script>
import TheNavbar from '@/components/TheNavbar.vue'
import TheFooter from '@/components/TheFooter.vue'

export default {
  name: 'App',
  components: {
    TheNavbar,
    TheFooter
  },
  data() {
    return {
      isLoading: false,
      showBackToTop: false,
      isDarkTheme: false
    }
  },
  provide() {
    return {
      isDarkTheme: this.isDarkTheme,
      toggleTheme: this.toggleTheme
    }
  },
  mounted() {
    // 监听滚动事件
    window.addEventListener('scroll', this.handleScroll)
    
    // 监听路由变化，显示加载状态
    this.$router.beforeEach((to, from, next) => {
      this.isLoading = true
      next()
    })
    
    this.$router.afterEach(() => {
      setTimeout(() => {
        this.isLoading = false
      }, 300)
    })
    
    // 初始化主题
    this.initializeTheme()
    
    // 预加载图片
    this.preloadImages()
  },
  beforeUnmount() {
    window.removeEventListener('scroll', this.handleScroll)
  },
  methods: {
    handleScroll() {
      const scrollTop = window.pageYOffset || document.documentElement.scrollTop
      this.showBackToTop = scrollTop > 300
    },
    scrollToTop() {
      window.scrollTo({
        top: 0,
        behavior: 'smooth'
      })
    },
    initializeTheme() {
      // 从localStorage读取主题设置
      const savedTheme = localStorage.getItem('theme')
      this.isDarkTheme = savedTheme === 'dark'
      this.applyTheme()
    },
    toggleTheme() {
      this.isDarkTheme = !this.isDarkTheme
      this.applyTheme()
      localStorage.setItem('theme', this.isDarkTheme ? 'dark' : 'light')
    },
    applyTheme() {
      if (this.isDarkTheme) {
        document.body.classList.add('dark-theme')
        document.documentElement.setAttribute('data-theme', 'dark')
      } else {
        document.body.classList.remove('dark-theme')
        document.documentElement.setAttribute('data-theme', 'light')
      }
    },
    preloadImages() {
      // 预加载重要图片
      const images = [
        require('@/assets/background.jpg'),
        require('@/assets/head.jpg'),
        require('@/assets/log.png')
      ]
      
      images.forEach(src => {
        const img = new Image()
        img.src = src
      })
    }
  }
}
</script>

<style>
/* 全局样式 */
* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  padding: 0;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  line-height: 1.6;
  color: #333;
  background-color: #fff;
  overflow-x: hidden;
}

#app {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  position: relative;
}

/* 固定背景 */
body::before {
  content: '';
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url('@/assets/background.jpg') center center/cover no-repeat;
  opacity: 0.6;
  z-index: -2;
}

body::after {
  content: '';
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.70) 0%, rgba(248, 249, 250, 0.75) 100%);
  z-index: -1;
}

main {
  padding-top: 80px; /* 为导航栏留出空间 */
  min-height: calc(100vh - 80px);
}

/* 页面切换动画 */
.page-enter-active,
.page-leave-active {
  transition: all 0.4s ease;
}

.page-enter-from {
  opacity: 0;
  transform: translateY(20px);
}

.page-leave-to {
  opacity: 0;
  transform: translateY(-20px);
}

/* 加载指示器 */
.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(255, 255, 255, 0.9);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
  backdrop-filter: blur(5px);
}

.loading-spinner {
  text-align: center;
  color: #6c757d;
}

/* 返回顶部按钮 */
.back-to-top-btn {
  position: fixed;
  bottom: 30px;
  right: 30px;
  width: 50px;
  height: 50px;
  background: #0d6efd;
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(13, 110, 253, 0.3);
  transition: all 0.3s ease;
  z-index: 1000;
}

.back-to-top-btn:hover {
  background: #0b5ed7;
  transform: translateY(-3px);
  box-shadow: 0 8px 20px rgba(13, 110, 253, 0.4);
}

.back-to-top-btn i {
  font-size: 1.2rem;
}

/* 自定义滚动条 */
::-webkit-scrollbar {
  width: 8px;
}

::-webkit-scrollbar-track {
  background: #f1f1f1;
}

::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}

/* 响应式字体 */
@media (max-width: 768px) {
  .back-to-top-btn {
    bottom: 20px;
    right: 20px;
    width: 45px;
    height: 45px;
  }
  
  main {
    padding-top: 70px;
  }
}

/* 深色主题支持 */
.dark-theme,
[data-theme="dark"] {
  background-color: #121212 !important;
  color: #ffffff !important;
}

.dark-theme body::before,
[data-theme="dark"] body::before {
  opacity: 0.2;
}

.dark-theme body::after,
[data-theme="dark"] body::after {
  background: linear-gradient(135deg, rgba(18, 18, 18, 0.85) 0%, rgba(33, 37, 41, 0.90) 100%);
}

.dark-theme .loading-overlay,
[data-theme="dark"] .loading-overlay {
  background: rgba(18, 18, 18, 0.9);
  color: #ffffff;
}

/* 深色主题下的卡片样式 */
.dark-theme .card,
[data-theme="dark"] .card {
  background: rgba(33, 37, 41, 0.85) !important;
  border-color: rgba(255, 255, 255, 0.1) !important;
  color: #ffffff;
}

.dark-theme .card:hover,
[data-theme="dark"] .card:hover {
  background: rgba(33, 37, 41, 0.95) !important;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.25);
}

/* 深色主题下的按钮样式 */
.dark-theme .btn-primary,
[data-theme="dark"] .btn-primary {
  background: linear-gradient(45deg, #66b3ff, #4285f4);
  border-color: #66b3ff;
}

.dark-theme .btn-primary:hover,
[data-theme="dark"] .btn-primary:hover {
  background: linear-gradient(45deg, #4da6ff, #3367d6);
  border-color: #4da6ff;
}

.dark-theme .btn-outline-primary,
[data-theme="dark"] .btn-outline-primary {
  border-color: #66b3ff;
  color: #66b3ff;
}

.dark-theme .btn-outline-primary:hover,
[data-theme="dark"] .btn-outline-primary:hover {
  background-color: #66b3ff;
  border-color: #66b3ff;
  color: #121212;
}

/* 深色主题下的表单控件 */
.dark-theme .form-control,
.dark-theme .form-select,
[data-theme="dark"] .form-control,
[data-theme="dark"] .form-select {
  background-color: #2c3034;
  border-color: #495057;
  color: #ffffff;
}

.dark-theme .form-control:focus,
.dark-theme .form-select:focus,
[data-theme="dark"] .form-control:focus,
[data-theme="dark"] .form-select:focus {
  background-color: #2c3034;
  border-color: #66b3ff;
  color: #ffffff;
  box-shadow: 0 0 0 0.25rem rgba(102, 179, 255, 0.25);
}

.dark-theme .form-control::placeholder,
[data-theme="dark"] .form-control::placeholder {
  color: #adb5bd;
}

/* 深色主题下的返回顶部按钮 */
.dark-theme .back-to-top-btn,
[data-theme="dark"] .back-to-top-btn {
  background: #66b3ff;
  box-shadow: 0 4px 12px rgba(102, 179, 255, 0.3);
}

.dark-theme .back-to-top-btn:hover,
[data-theme="dark"] .back-to-top-btn:hover {
  background: #4da6ff;
  box-shadow: 0 8px 20px rgba(102, 179, 255, 0.4);
}

/* 深色主题下的滚动条 */
.dark-theme ::-webkit-scrollbar-track,
[data-theme="dark"] ::-webkit-scrollbar-track {
  background: #2c3034;
}

.dark-theme ::-webkit-scrollbar-thumb,
[data-theme="dark"] ::-webkit-scrollbar-thumb {
  background: #495057;
}

.dark-theme ::-webkit-scrollbar-thumb:hover,
[data-theme="dark"] ::-webkit-scrollbar-thumb:hover {
  background: #6c757d;
}

/* 深色主题下的文本选择 */
.dark-theme ::selection,
[data-theme="dark"] ::selection {
  background: rgba(102, 179, 255, 0.3);
  color: inherit;
}

.dark-theme ::-moz-selection,
[data-theme="dark"] ::-moz-selection {
  background: rgba(102, 179, 255, 0.3);
  color: inherit;
}

/* 深色主题下的链接 */
.dark-theme a,
[data-theme="dark"] a {
  color: #66b3ff;
}

.dark-theme a:hover,
[data-theme="dark"] a:hover {
  color: #4da6ff;
}

/* 深色主题下的导航栏样式 */
.dark-theme .navbar-scrolled.bg-dark,
[data-theme="dark"] .navbar-scrolled.bg-dark {
  background-color: rgba(33, 37, 41, 0.95) !important;
  border-bottom: 1px solid rgba(255,255,255,0.1);
}

.dark-theme .navbar-dark .nav-link,
[data-theme="dark"] .navbar-dark .nav-link {
  color: rgba(255,255,255,0.75) !important;
}

.dark-theme .navbar-dark .nav-link:hover,
[data-theme="dark"] .navbar-dark .nav-link:hover {
  color: #66b3ff !important;
}

.dark-theme .navbar-dark .nav-link.active,
[data-theme="dark"] .navbar-dark .nav-link.active {
  color: #66b3ff !important;
  background: rgba(102, 179, 255, 0.1);
}

.dark-theme .navbar-dark .nav-link.active::after,
[data-theme="dark"] .navbar-dark .nav-link.active::after {
  background: #66b3ff;
}

.dark-theme .search-input,
[data-theme="dark"] .search-input {
  background-color: #2c3034 !important;
  border-color: #495057 !important;
  color: #fff !important;
}

.dark-theme .search-input:focus,
[data-theme="dark"] .search-input:focus {
  background-color: #2c3034 !important;
  border-color: #66b3ff !important;
  color: #fff !important;
  box-shadow: 0 0 0 0.25rem rgba(102, 179, 255, 0.25) !important;
}

/* 深色主题下的移动端导航样式 */
@media (max-width: 991px) {
  .dark-theme .navbar-nav,
  [data-theme="dark"] .navbar-nav {
    border-color: rgba(255,255,255,0.1);
  }
  
  .dark-theme .navbar-tools,
  [data-theme="dark"] .navbar-tools {
    border-color: rgba(255,255,255,0.1);
  }
}

/* 卡片和内容区域通用样式 */
.card {
  background: rgba(255, 255, 255, 0.80) !important;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.3) !important;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
}

.card:hover {
  background: rgba(255, 255, 255, 0.90) !important;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

/* 按钮通用样式增强 */
.btn {
  border-radius: 0.5rem;
  font-weight: 500;
  padding: 0.6rem 1.2rem;
  transition: all 0.3s ease;
}

.btn:focus {
  box-shadow: none;
}

.btn-primary {
  background: linear-gradient(45deg, #0d6efd, #4285f4);
  border: none;
}

.btn-primary:hover {
  background: linear-gradient(45deg, #0b5ed7, #3367d6);
  transform: translateY(-1px);
}

/* 表单控件样式增强 */
.form-control,
.form-select {
  border-radius: 0.5rem;
  border: 2px solid #e9ecef;
  padding: 0.75rem 1rem;
  transition: all 0.3s ease;
}

.form-control:focus,
.form-select:focus {
  border-color: #86b7fe;
  box-shadow: 0 0 0 0.25rem rgba(13, 110, 253, 0.25);
}

/* 动画关键帧 */
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes fadeInDown {
  from {
    opacity: 0;
    transform: translateY(-30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

/* 实用类 */
.animate-fadeInUp {
  animation: fadeInUp 0.8s ease-out;
}

.animate-fadeInDown {
  animation: fadeInDown 0.8s ease-out;
}

.animate-fadeIn {
  animation: fadeIn 0.6s ease-out;
}

/* 文本选择样式 */
::selection {
  background: rgba(13, 110, 253, 0.2);
  color: inherit;
}

::-moz-selection {
  background: rgba(13, 110, 253, 0.2);
  color: inherit;
}

/* 链接样式统一 */
a {
  color: #0d6efd;
  text-decoration: none;
  transition: color 0.3s ease;
}

a:hover {
  color: #0b5ed7;
  text-decoration: underline;
}

/* 打印样式 */
@media print {
  .navbar,
  .footer,
  .back-to-top-btn,
  .loading-overlay {
    display: none !important;
  }
  
  main {
    padding-top: 0 !important;
  }
  
  body::before,
  body::after {
    display: none !important;
  }
}
</style>
